define(['zepto', 'butterfly/view','../../bsl',
    'text!./runtime.html'
], function($, View, bsl, runtime) {
    var me = null;

    var view = View.extend({
        events: {
            "tap #back":"back",
            "tap #getVersion":"getVersion",
            "tap #restartApp":"restartApp",
            "tap #getWidgetInfo":"getWidgetInfo",
            "tap #setBadge":"setBadge",
            "tap #clearBadge":"clearBadge",
            "tap #exitApp":"exitApp",
            "tap #clicked":"clicked",
            "tap #openDoc":"OpenDoc"
            
        },
        render: function() {
            me = this;
        },

        onShow: function() {

            // 绑定自定义事件 滑动切换日历等           


        },
        getVersion: function(){
            console.info( "程序版本号："+bsl.runtime.version+"\n内核版本号："+bsl.runtime.innerVersion );
        },

        restartApp: function() {
            bsl.runtime.restart();
        },

        getWidgetInfo: function() {
            bsl.runtime.getProperty( bsl.runtime.appid, function ( wgtinfo ) {
                //appid属性
                var wgtStr = "appid:"+wgtinfo.appid;
                //version属性
                wgtStr += "\nversion:"+wgtinfo.version;
                //name属性
                wgtStr += "\nname:"+wgtinfo.name;
                //description属性
                wgtStr += "\ndescription:"+wgtinfo.description;
                //author属性
                wgtStr += "\nauthor:"+wgtinfo.author;
                //email属性
                wgtStr += "\nemail:"+wgtinfo.email;
                //licence属性
                wgtStr += "\nlicense:"+wgtinfo.license;
                //licensehref属性
                wgtStr += "\nlicensehref:"+wgtinfo.licensehref;
                //features 属性
                wgtStr += "\nfeatures:"+wgtinfo.features;
                console.info( wgtStr );
            } );
        },

        setBadge: function() {
            bsl.runtime.setBadgeNumber( 50 );
            console.info( "设置程序图标右上角显示的提示数字为50\n请返回桌面查看" );
            if(bsl.os.name=="iOS"){
                console.info( '*如果无法设置提示数字，请到"设置"->"通知"中配置应用在通知中心显示!' );
            }else{
                console.info( "注：仅支持小米(MIUI v5)，其它设备暂不支持此功能!" );
            }
        },

        clearBadge: function() {
            bsl.runtime.setBadgeNumber( 0 );
            console.info( "清除程序图标右上角显示的提示数字\n请返回桌面查看" );
            if(bsl.os.name=="iOS"){
                console.info( '如果无法清除提示数字，请到"设置"->"通知"中配置应用在通知中心显示!' );
            }else{
                console.info( "注：仅支持小米(MIUI v5)，其它设备暂不支持此功能!" );
            }
        },

        exitApp: function(){
            if(bsl.os.name=="Android"){
                bsl.runtime.quit();
            }else{
                console.info( "此平台不支持直接退出程序，请按Home键切换应用" );
            }
        },

        updateApp: function(){
            var url='http://demo.dcloud.net.cn/helloh5/update/HelloH5.wgtu';
            bsl.nativeUI.showWaiting("升级中...");
            var dtask = bsl.downloader.createDownload( url, {method:"GET"}, function(d,status){
                if ( status == 200 ) { 
                    console.log( "Download wgtu success: " + d.filename );
                    bsl.runtime.install(d.filename,{},function(){
                        bsl.nativeUI.closeWaiting();
                        bsl.nativeUI.alert("Update wgtu success, restart now!",function(){
                            bsl.runtime.restart();
                        });
                    },function(e){
                        bsl.nativeUI.closeWaiting();
                        alert("Update wgtu failed: "+e.message);
                    });
                } else {
                    bsl.nativeUI.closeWaiting();
                     alert( "Download wgtu failed: " + status ); 
                } 
            } );
            dtask.addEventListener('statechanged',function(d,status){
                console.log("statechanged: "+d.state);
            });
            dtask.start();
        },

        back: function(e){
            window.history.go(-1);
        },

        clicked: function(e){
            window.butterfly.navigate('runtime_launch.html', {
                trigger: true
            });
        },

        OpenDoc : function(e){
           
            window.butterfly.navigate('com.no5.cordovademo/doc/runtime', {
                trigger: true
            });
        },
    });
    return view;
});
